/**
 * @class tuan.controller.DashboardController
 * @extends extendsClass
 * Description
 */
 /**
  * @class MyNamespace.DashboardController
  * @extends extendsClass
  * Description
  */
  Ext.define('tuan.controller.DashboardController', {
  	extend: 'Ext.app.Controller',
  	requires: [
  	'Ext.window.MessageBox'
  	],

  	refs : [
  	],
  	views : ["dashboard.dashboardView"],
  	stores : [],
  	init : function(){
  		this.control({
  			'dashboard-Master' : {
  				beforerender : this.loadDasboard
  			}
  		})
  	},
  	actionIndex : function(){
  		panel = Ext.getCmp("dashboard-wapper");
  		panel.add({xtype : 'dashboard-Master'})
  	},
  	loadDasboard : function(){
  		// console.log( tuan.config.Runtime.linkUrl);
  		this.loadDasboardInfo();
  		setInterval(function(){
  			Ext.Ajax.on("beforerequest", function(){
  				window.parent.Ext.getBody().unmask();
  			});
  			Ext.Ajax.on("requestcomplete", function(){
  				window.parent.Ext.getBody().unmask();
  			});
  			Ext.Ajax.request({
  				// url: 'http://localhost/cafe_shop_control/index.php/ThongKeDonHang/thongTinDasboard',
  				url : tuan.config.Runtime.linkUrl+'ThongKeDonHang/thongTinDasboard',
  				success: function(response, opts) {
  					var obj = Ext.decode(response.responseText);
			      // console.dir(obj.lists[0].total);
			      totalToday = obj.lists[0].total;
			      totalToday =  Ext.util.Format.currency(totalToday, 'VND ');
			      totalChuaThanhToan = obj.lists[1].total;
			      totalChuaThanhToan = Ext.util.Format.currency(totalChuaThanhToan, 'VND ');
			      totalMonth = obj.lists[2].total;
			      totalMonth = Ext.util.Format.currency(totalMonth, 'VND ');
			      totalNew = obj.lists[3].total;

			      panel = Ext.ComponentQuery.query("#dashboard-order")[0];
			      panel.update('');
			      html = "<div class='dashboard-class'><p><span>Tổng số tiền đặt hàng hôm nay </span><b>"+totalToday+"</b></p>"
			      +"<p><span>Tổng số tiền đã thanh toán  </span><b>"+ totalChuaThanhToan+"</b></p>"
			      +"<p><span>Tổng số đã order trong tháng  </span><b>"+totalMonth+"</b></p>"
			         +"<span class='new-order'>"+totalNew+" new orders</span></div>";
			      panel.update(html);
			  },
			  failure: function(response, opts) {
			  	console.log('server-side failure with status code ' + response.status);
			  }
			});
		},10000);

		// thong tin nguoi lam viec
		this.loadUserInfo();
},
loadDasboardInfo : function(){
	Ext.Ajax.on("beforerequest", function(){
		window.parent.Ext.getBody().unmask();
	});
	Ext.Ajax.on("requestcomplete", function(){
		window.parent.Ext.getBody().unmask();
	});
	Ext.Ajax.on("requestexception", function(){
		Ext.Msg.alert('Error','Sorry something wrong!');
		window.parent.Ext.getBody().unmask();
	});
	Ext.Ajax.request({
		url: tuan.config.Runtime.linkUrl+'ThongKeDonHang/thongTinDasboard',
		success: function(response, opts) {
			var obj = Ext.decode(response.responseText);
		      // console.dir(obj.lists[0].total);
		      totalToday = obj.lists[0].total;
		      totalToday =  Ext.util.Format.currency(totalToday, 'VND ');
		      totalChuaThanhToan = obj.lists[1].total;
		      totalChuaThanhToan = Ext.util.Format.currency(totalChuaThanhToan, 'VND ');
		      totalMonth = obj.lists[2].total;
		      totalMonth = Ext.util.Format.currency(totalMonth, 'VND ');
		      totalNew = obj.lists[3].total;

		      panel = Ext.ComponentQuery.query("#dashboard-order")[0];
		      panel.update('');
		      html = "<div class='dashboard-class'><p><span>Tổng số tiền đặt hàng hôm nay </span><b>"+totalToday+"</b></p>"
		      +"<p><span>Tổng số tiền đã thanh toán  </span><b>"+ totalChuaThanhToan+"</b></p>"
		      +"<p><span>Tổng số đã order trong tháng  </span><b>"+totalMonth+"</b></p>"
		     +"<span class='new-order'>"+totalNew+" new orders</span></div>";
		      panel.update(html);
		  },
		  failure: function(response, opts) {
		  	console.log('server-side failure with status code ' + response.status);
		  }
		});
},
loadUserInfo : function(){
	Ext.Ajax.request({
		url: tuan.config.Runtime.linkUrl+'Login/getInfoUser',
	    params: {
            username : sessionStorage.getItem('username')
            // action   : "check_login_action",
        },
		success: function(response, opts) {
			var obj = Ext.decode(response.responseText);
			var roles = obj.roles;
			var fullname = obj.fullname;
			sessionStorage.setItem('fullname',fullname);
			var username = obj.username;
			var lastlogin = obj.last_login;
			chucvu = '';
			for(var r in roles){
				console.log(roles[r].role_name);
				chucvu += ( parseInt(r) +1) +'.'+ roles[r].role_name+' ';
			}
			panel = Ext.ComponentQuery.query("#dashboard-thongtin-user")[0];
			panel.update('');
			html = "<div class='dashboard-class'>"
			+"<p>Xin chào : "+username+"</p>"
			+"<p>Tên họ nhân viên : "+fullname+"</p>"
						+"<p>Chức vụ : "+chucvu+"</p>"
			+"<p>Truy cập vào lúc : "+lastlogin+"</p>"
			+"</div>";
			panel.update(html);
		  },
		  failure: function(response, opts) {
		  	console.log('server-side failure with status code ' + response.status);
		  }
		});
}
});